<template>
  <a-modal
    :title="title"
    :width="700"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :okText="readOnly?'确定核销':'确定'"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="车牌号">
          <a-input
            placeholder="请输入车牌号"
            :disabled="readOnly"
            v-decorator="['plateNo', {rules: [{ type: 'string',required: true, message: '请输入车牌号!' }],}]"/>
        </a-form-item>

        <a-form-item v-show="false" :labelCol="labelCol" :wrapperCol="wrapperCol" label="是否是白名单">
          <a-select   v-decorator="['licenseType', {initialValue:3}]" placeholder="请选择是否是白名单" :disabled="readOnly">
            <a-select-option :value="1">白名单</a-select-option>
            <a-select-option :value="2">黑名单</a-select-option>
            <a-select-option :value="3">储值车</a-select-option>
          </a-select>
        </a-form-item>


        <a-form-item v-show="false" :labelCol="labelCol" :wrapperCol="wrapperCol" label="是否启用">
          <a-select v-decorator="['status', {initialValue:'Y'}]" :disabled="readOnly">
            <a-select-option :value="'Y'">启用</a-select-option>
            <a-select-option :value="'N'">冻结</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="选择车辆组类型">
          <a-select
              :disabled="readOnly"
            :options="carTypes"
            v-decorator="['carTypeId', {initialValue:defaultCarTypeId}]"
            placeholder="请选择储值车类型">
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="{
            xs: {span: 24},
            sm: {span: 5},
          }"
          :wrapperCol="{
            xs: {span: 24},
            sm: {span: 13},
          }"
          label="客户姓名">
          <a-input
              :disabled="readOnly"
            placeholder="请输入客户姓名"
            v-decorator="['userName', {rules: [{ type: 'string', required: true, message: '请输入客户姓名!' },{max:40,message: '客户姓名长度不要超过40个字符'}]}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="{
            xs: {span: 24},
            sm: {span: 5},
          }"
          :wrapperCol="{
            xs: {span: 24},
            sm: {span: 13},
          }"
          label="客户手机号码">
          <a-input
              :disabled="readOnly"
            placeholder="请输入客户手机号码"
            v-decorator="['mobile', {rules: [{required: false,message: '请输入客户手机号'},{ validator:this.validateMobile}],}]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="payAlias">
          <!--          支付费用(元)-->
          <a-input-number
            :min="0"
            :max="maxPayFee"
            :disabled="disableBalance"
            v-decorator="[ 'payFee', {rules: [{required: true, message: '请输入'+payAlias+'!' }],initialValue:0}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="家庭住址">
          <a-input
              :disabled="readOnly"
              placeholder="请输入家庭住址"
            v-decorator="['address', {rules:[{max:100,message:'家庭住址长度不要超过100个字符'}]}]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="车辆备注">
          <a-input
              :disabled="readOnly"

              placeholder="请输入车辆备注"
            v-decorator="['remark', {rules:[{max:100,message:'车辆备注长度不要超过100个字符'}]}]"/>
        </a-form-item>

        <a-form-item
          v-show="false"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="修改时间">
          <a-date-picker
              :disabled="readOnly"
              updateTime
            format="YYYY-MM-DD HH:mm:ss"
            v-decorator="[ 'updateTime', {}]"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import {httpAction, postAction} from '@api/manage'
import pick from 'lodash.pick'
import moment from "moment"

export default {
  name: "ParkStoreListModal",
  data() {
    return {
      title: "操作",
      payAlias: '支付费用(元)',
      visible: false,
      disableBalance: false,
      readOnly:false,
      model: {},
      labelCol: {
        xs: {span: 24},
        sm: {span: 5},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },
      parkList: [],
      //  carTypes: [{value:'',label:'请选择车辆'}],
      carTypes: [],
      parkCode: '',
      parkName: '',
      defaultCarTypeId: '',
      maxPayFee:0,
      confirmLoading: false,
      form: this.$form.createForm(this),
      url: {
        add: "/parkCar/parkCarStored/add",
        edit: "/parkCar/parkCarStored/edit",
        getMaxFee:'/parkCar/parkCarStored/getMaxFee',
        writeOff: "/parkCar/parkCarStored/writeOff",
      },
    }
  },
  mounted() {

  },
  methods: {
    add() {
      this.edit({});
    },
    edit(record, judge) {
      this.disableBalance = false;
      this.payAlias = '支付费用(元)';
      if (judge === 'pay' || judge === 'edit') {
        this.disableBalance = true;
        this.payAlias = '余额(元)';
      }
      this.visible = true;
      this.confirmLoading = true
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'plateNo', 'licenseType', 'carTypeId', 'userName', 'mobile', 'status', 'payFee', 'carDeposit', 'address', 'remark'))
        //时间格式化
        this.form.setFieldsValue({updateTime: this.model.updateTime ? moment(this.model.updateTime) : null})
        // this.form.setFieldsValue({payFee:this.model.payFee+this.model.recharge})
        this.loadMaxPay();
      });

    },
    close() {
      this.$emit('Refresh');
      this.visible = false;
    },
    handleOk() {
      // 如果是核销的话，那么就走核销的流程
      if(this.readOnly){
        // 是核销
        if(this.model.id===''||this.model.id===undefined||this.model.id==null){
          that.$message.error("未找到相关信息！请稍后重试！！");
          return;
        }
        // 走核销的流程
        postAction(this.url.writeOff,{id:this.model.id}).then(res=>{
          if(res.success)
          {
            this.$message.success("核销该储值车信息成功")
            this.$emit('Refresh');
            this.close()
          }
          else {
            this.$message.error("核销失败!"+res.message)
          }
        })
        return;
      }
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          let formData = Object.assign(this.model, values);
          //时间格式化
          formData.updateTime = formData.updateTime ? formData.updateTime.format('YYYY-MM-DD HH:mm:ss') : null;
          this.model.recharge = this.model.recharge ? this.model.recharge : 0;
          formData.payFee = this.model.payFee + this.model.recharge
          if (formData.carTypeId == "") {
            this.$message.error("请添加储值车类型");
            return
          } else {
            this.confirmLoading = true
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
                that.close();
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              this.confirmLoading = false
            })
          }
        }
      })
    },
    handleCancel() {
      this.close()
    },
    loadMaxPay() {
      const that = this
      postAction(this.url.getMaxFee).then(res=>{
        if(res.success)
        {
          that.maxPayFee = res.result
          this.confirmLoading = false
          this.visible = true
          this.fee = 0
        }
        else {
          this.$message.error("未获取储值车的最大余额")
        }
      })
    },
    validateMobile(rule, value, callback) {
      if (!value || new RegExp(/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/).test(value)) {
        callback();
      } else {
        callback("手机号码格式不正确!");
      }
    }
  }
}
</script>

<style lang="less" scoped>

</style>
